import store from '@/store'
import {
  VuexModule,
  Module,
  Mutation,
  Action,
  getModule
} from 'vuex-module-decorators'

interface ErrorLogType {
  err: Error
  info?: string
  url: string
  date: Date
}

export interface ErrorLogState {
  logs: Array<ErrorLogType>
}

@Module({ store, dynamic: true, namespaced: true, name: 'errorLog' })
class ErrorLog extends VuexModule implements ErrorLogState {
  // state
  public logs: Array<ErrorLogType> = []

  // mutations
  @Mutation
  private ADD_ERROR_LOG (log: ErrorLogType) {
    this.logs.push(log)
  }

  @Mutation
  private CLEAR_ERROR_LOG () {
    this.logs.length = 0
  }

  // actions
  @Action
  public addErrorLog (log: ErrorLogType) {
    this.ADD_ERROR_LOG(log)
  }

  @Action
  public clearErrorLog () {
    this.CLEAR_ERROR_LOG()
  }
}

export default getModule(ErrorLog)
